package com.itianyu.blog.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.itianyu.blog.dao.beans.Comment;
import com.itianyu.blog.dao.beans.Entry;
import com.itianyu.blog.dao.beans.Tag;
import com.itianyu.blog.dao.beans.User;
import com.itianyu.blog.dao.interfaces.CommentInterface;
import com.itianyu.blog.dao.interfaces.EntryInterface;
import com.itianyu.blog.dao.interfaces.TagInterface;

/**
 * 标签控制层
 * @author wumgji
 */
@Controller
@RequestMapping("/comment")
public class CommentController {

	@Autowired
	private TagInterface tagInterface;
	@Autowired
	private EntryInterface entryInterface;
	@Autowired
	private CommentInterface commentInterface;
	
	
	// 显示评论列表列表
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public ModelAndView save(String content,int entryid) throws Exception {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		User user= (User) authentication.getPrincipal();
		Entry entry=entryInterface.findEntry(entryid);
		Integer total=entry.getCommenttotal();
		if(total==null){
			entry.setCommenttotal(1);
		}else{
			entry.setCommenttotal(total+1);
		}
		entryInterface.saveEntry(entry);
		Comment comment=new Comment();
		comment.setContent(content);
		comment.setCreationDate(new Date());
		comment.setEntryid(entryid);
		comment.setEntrytitle(entry.getTitle());
		comment.setUsername(user.getUsername());
		commentInterface.save(comment);
		return new ModelAndView("commentlist", new ModelMap().addAttribute("comments", commentInterface.findCommentEntities(entryid)));
	}
	
	// 显示评论列表列表
	@RequestMapping(value = "/add/{entryid}", method = RequestMethod.GET)
	public ModelAndView add(@PathVariable("entryid") int entryid) throws Exception {
		return new ModelAndView("commentform", new ModelMap().addAttribute("entryid", entryid));
	}
   
	// 显示标签列表
	@RequestMapping(value = "/all/{entryId}", method = RequestMethod.GET)
	public ModelAndView listEntriesByDate(@PathVariable("entryId") int entryId) throws Exception {
		ModelMap info= new ModelMap();
		info.addAttribute("comments", commentInterface.findCommentEntities(entryId));
		info.addAttribute("entryId", entryId);
		return new ModelAndView("commentlist",info);
	}

}
